
#include "comm/znx_log.h"
#include "comm/znx_hash_tab.h"


typedef struct {
    znx_hash_node_t     node;
    int                 key;      
} znx_test_node_t;


static int
znx_test_node_cmp(znx_hash_node_t *node_a, znx_hash_node_t *node_b)
{
    znx_test_node_t *tnode_a, *tnode_b;
    tnode_a = (znx_test_node_t *)node_a;
    tnode_b = (znx_test_node_t *)node_b;

    if (tnode_a->key == tnode_b->key) {
        return 0;
    }

    if (tnode_a->key > tnode_b->key) {
        return 1;
    }

    return -1;
}


int main()
{
    znx_hash_tab_t      *htab;
    znx_test_node_t     *tnode;
    znx_test_node_t     target;
    znx_hash_node_t     *hnode;

    htab = znx_hash_tab_create(0, 0, znx_test_node_cmp, NULL);

    for (int i = 0; i < 1024; i++) {
        tnode = znx_thread_calloc(sizeof(znx_test_node_t));
        tnode->key = i;
        tnode->node.hash_key = (uint64_t)i;
        znx_hash_tab_add(htab, &tnode->node);
    }

    for (int i = 0; i < 1024; i++) {
        target.key = i;
        target.node.hash_key = (uint64_t)i;
        hnode = znx_hash_tab_get(htab, &target.node);
        if (hnode == NULL) {
            ZNX_DEF_LOG_FATAL(NULL, "not found %d node", i);
        }

        tnode = (znx_test_node_t *)hnode;
        ZNX_DEF_LOG_DEBUG(NULL, "found %d node, key: %d", i, tnode->key);
    }

    target.key = 100;
    target.node.hash_key = 100;
    hnode = znx_hash_tab_remove(htab, &target.node);
    if (hnode == NULL) {
        ZNX_DEF_LOG_FATAL(NULL, "not found 100 node");
    }
    znx_thread_free((znx_test_node_t *)hnode);

    hnode = znx_hash_tab_get(htab, &target.node);
    if (hnode != NULL) {
        ZNX_DEF_LOG_FATAL(NULL, "found a remove node");
    }

    for (int i = 0; i < 1024; i++) {
        target.key = i;
        target.node.hash_key = (uint64_t)i;
        hnode = znx_hash_tab_remove(htab, &target.node);
        if (hnode == NULL) {
            continue;
        }
        znx_thread_free(hnode);
    }


    znx_hash_tab_destroy(htab);

    if (likely(1)) {
        printf("11111");
    }

    return 0;
}